package com.cq.hd.member.service.impl;

import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.enums.biz.BillTypeEnum;
import com.cq.hd.common.enums.biz.FinancialAccountTypeEnum;
import com.cq.hd.common.enums.biz.MemberAmtBillTradeTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.member.api.dto.AdminMerchantAmtBillPageDto;
import com.cq.hd.member.api.dto.MerchantAmtBillDto;
import com.cq.hd.member.api.vo.AdminMerchantAmtBillPageVo;
import com.cq.hd.member.api.vo.MerchantAmtBillPageVo;
import com.cq.hd.member.api.vo.MerchantAmtBillVo;
import com.cq.hd.member.api.vo.MerchantVo;
import com.cq.hd.member.po.TbMerchantAmtBillPo;
import com.cq.hd.member.mapper.TbMerchantAmtBillMapper;
import com.cq.hd.member.service.TbMerchantAmtBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.member.service.TbMerchantService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户资金流水表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2024-01-28
 */
@Service
public class TbMerchantAmtBillServiceImpl extends ServiceImpl<TbMerchantAmtBillMapper, TbMerchantAmtBillPo> implements TbMerchantAmtBillService {

    @Resource
    private TbMerchantService merchantService;

    @Override
    public List<MerchantAmtBillVo> listMerchantAmtBill(MerchantAmtBillDto merchantAmtBillDto) {
        Long merchantId = merchantAmtBillDto.getMerchantId();
        if (merchantId == null || merchantId <= 0) {
            return null;
        }
        MerchantVo merchantVo = merchantService.getMerchantById(merchantId);
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startLocalDateTime = now.plusYears(-1);
        merchantAmtBillDto.setStartLocalDateTime(LocalDateUtil.toLocalDateTimeToString(startLocalDateTime, Constant.YYYY_MM_DD_HH_MM_SS));

        // 查询用户最近一年的佣金流水数据
        List<MerchantAmtBillPageVo> merchantAmtBillPageVos = baseMapper.listByDto(merchantAmtBillDto);
        if (!CollectionUtils.isEmpty(merchantAmtBillPageVos)) {
            Map<String, List<MerchantAmtBillPageVo>> map = new HashMap<>();

            for (MerchantAmtBillPageVo merchantAmtBillPageVo : merchantAmtBillPageVos) {
                if (FinancialAccountTypeEnum.SELF.getValue() == merchantVo.getFinancialAccountType()
                        && MemberAmtBillTradeTypeEnum.WITHDRAWAL_AMT.getValue().equals(merchantAmtBillPageVo.getTradeType())) {
                    continue;
                }

                LocalDateTime createTime = merchantAmtBillPageVo.getCreateTime();
                String date = LocalDateUtil.toLocalDateToString(createTime.toLocalDate(), "yyyy-MM");

                List<MerchantAmtBillPageVo> merchantAmtBillPageVoList = new ArrayList<>();
                if (map.containsKey(date)) {
                    merchantAmtBillPageVoList = map.get(date);
                }

                merchantAmtBillPageVoList.add(merchantAmtBillPageVo);
                map.put(date, merchantAmtBillPageVoList);
            }

            List<MerchantAmtBillVo> merchantAmtBillVos = new ArrayList<>();

            for (String date : map.keySet()) {
                List<MerchantAmtBillPageVo> merchantAmtBillPageVoList = map.get(date);

                BigDecimal monthIncome = BigDecimal.ZERO;
                BigDecimal monthExpend = BigDecimal.ZERO;
                for (MerchantAmtBillPageVo merchantAmtBillPageVo : merchantAmtBillPageVoList) {
                    BigDecimal tradeAmt = merchantAmtBillPageVo.getTradeAmt();
                    Integer getBillType = merchantAmtBillPageVo.getBillType();
                    if (BillTypeEnum.INCOME.getValue().equals(getBillType)) {
                        monthIncome = monthIncome.add(tradeAmt);
                    } else if (BillTypeEnum.EXPEND.getValue().equals(getBillType)) {
                        monthExpend = monthExpend.add(tradeAmt);
                    }
                }

                String[] splitDate = date.split("-");

                MerchantAmtBillVo merchantAmtBillVo = new MerchantAmtBillVo();
                merchantAmtBillVo.setYear(splitDate[0]);
                merchantAmtBillVo.setMonth(splitDate[1]);
                merchantAmtBillVo.setMonthIncome(monthIncome);
                merchantAmtBillVo.setMonthExpend(monthExpend);
                merchantAmtBillVo.setMerchantAmtBills(merchantAmtBillPageVoList);
                merchantAmtBillVos.add(merchantAmtBillVo);
            }

            return merchantAmtBillVos;
        }

        return null;
    }

    @Override
    public Page<AdminMerchantAmtBillPageVo> pageMerchantAmtBill(AdminMerchantAmtBillPageDto adminMerchantAmtBillPageDto) {
        PageHelper.startPage(adminMerchantAmtBillPageDto.getPageNum(), adminMerchantAmtBillPageDto.getPageSize());
        List<AdminMerchantAmtBillPageVo> adminMerchantAmtBillPageVos = baseMapper.listMerchantAmtBillByDto(adminMerchantAmtBillPageDto);
        PageInfo<AdminMerchantAmtBillPageVo> pageInfo = new PageInfo<>(adminMerchantAmtBillPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }
}
